import {getEnterpriseId} from "@/utils/getEnterpriseId"

export default class UrlFactory {

  // private property  

  static getInstance() {
    if (!this.instance) {
      this.instance = new UrlFactory();
    }
    return this.instance
  }

  info = {}
  parseUrlStr(encodedUrlStr) {
    let searchObj;
    let searchStr;
    let enterpriseId;

    const [url, str] = encodedUrlStr.split(".");
    if (str !== undefined) {
      searchStr = str.replace(/-/g, "&").replace(/_/g, "=");
      searchObj = querystring.parse(searchStr);
    } else {
      searchStr = "";
      searchObj = {};
    }

    let enterpriseIdArr = url.replace(/\//, "").match(/^\d{1,}/);
    if (enterpriseIdArr == null) {
      enterpriseId = "";
    } else {
      enterpriseId = enterpriseIdArr[0];
    }

    return {
      fullUrl: url + (searchStr != "" ? "?" : "") + searchStr,
      firstUrl: url,
      searchObj,
      enterpriseId,
    };
  }

  // 如 对https://yftx-map-dev.xailife.com/246/fuel/remit/?stationId=321&stationName=test342&userId=742&utmSource=1 进行编码
  // 编码后的 originUrl = /246/fuel/remit/.stationId_321-stationName_test342-userId_742-utmSource_1
  encodeUrlStr(){
    const enterpriseId = getEnterpriseId();
    const path = window.location.pathname === "/authen" ? ("/" + enterpriseId) : window.location.pathname
    const searchStr = window.location.search.replace("?", ".").replace(/&/g, "-").replace(/=/g, "_")
    const originUrl = path + searchStr;
    return originUrl
  }
}
